﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using SDM.ExamPlanner.BLL;
using SDM.ExamPlanner.DTO;

namespace sdm.exam.ui.LocationAdministration
{
    /// <summary>
    /// Interaction logic for AdministrateExamLocations.xaml
    /// </summary>
    public partial class AdministrateExamLocations : UserControl
    {
        readonly Manager<Classroom> _classroomManager = new Manager<Classroom>(); 
        Dictionary<int, string> _classroomDictionary = new Dictionary<int, string>();
        Dictionary<int, string> _classroomAssignedDictionary = new Dictionary<int, string>();
        public AdministrateExamLocations()
        {
            InitializeComponent();
            PopulateDropdown();
            PopulateList();
        }

        private void PopulateList()
        {
            _classroomAssignedDictionary = _classroomManager.Find()
                .Where(cl => cl.IsAssignableForExam == true)
                .ToDictionary(item => item.Id, item => item.Name);
            lstAssignedRooms.DataContext = _classroomAssignedDictionary.Values;
        }

        private void PopulateDropdown()
        {
            _classroomDictionary = _classroomManager.Find()
                .Where(cl=>cl.IsAssignableForExam==false)
                .ToDictionary(item=>item.Id,item=>item.Name);
            cbbClassRooms.DataContext = _classroomDictionary.Values;
        }

        private void btnAdd_Click(object sender, RoutedEventArgs e)
        {
            KeyValuePair<int, string> kvPair =
                _classroomDictionary.FirstOrDefault(i => i.Value.Equals(cbbClassRooms.SelectedValue));
            Classroom cr = _classroomManager.Find().FirstOrDefault(i => i.Id == kvPair.Key);
            if (cr != null)
            {
                cr.IsAssignableForExam = true;
                _classroomManager.Save(cr);
            }
            MessageBox.Show("Room is marked as exam room", "Success", MessageBoxButton.OK, MessageBoxImage.Information);
        }

        private void lstAssignedRooms_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            btnRemove.IsEnabled = lstAssignedRooms.SelectedItems.Count > 0;
        }

        private void btnRemove_Click(object sender, RoutedEventArgs e)
        {
            KeyValuePair<int, string> kvPair =
                _classroomAssignedDictionary.FirstOrDefault(i => i.Value.Equals(lstAssignedRooms.SelectedValue));
            Classroom cr = _classroomManager.Find().FirstOrDefault(i => i.Id == kvPair.Key);
            if (cr != null)
            {
                cr.IsAssignableForExam = false;
                _classroomManager.Save(cr);
            }
            MessageBox.Show("Room is unmarked as exam room", "Success", MessageBoxButton.OK, MessageBoxImage.Information);
        }
    }
}
